<html>
<head><meta charset="utf-8"><title>Vec::truncate implementation · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation.html">Vec::truncate implementation</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222147821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Atruncate%20implementation/near/222147821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation.html#222147821">(Jan 09 2021 at 02:06)</a>:</h4>
<p>I was just looking at the implementation of <code>Vec::truncate</code> and noticed that it doesn't return early if <code>len == self.len()</code>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">truncate</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">len</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// This is safe because:</span>
<span class="w">        </span><span class="c1">//</span>
<span class="w">        </span><span class="c1">// * the slice passed to `drop_in_place` is valid; the `len &gt; self.len`</span>
<span class="w">        </span><span class="c1">//   case avoids creating an invalid slice, and</span>
<span class="w">        </span><span class="c1">// * the `len` of the vector is shrunk before calling `drop_in_place`,</span>
<span class="w">        </span><span class="c1">//   such that no value will be dropped twice in case `drop_in_place`</span>
<span class="w">        </span><span class="c1">//   were to panic once (if it panics twice, the program aborts).</span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="n">len</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">len</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="k">return</span><span class="p">;</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">remaining_len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">len</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">len</span><span class="p">;</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ptr</span>::<span class="n">slice_from_raw_parts_mut</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">().</span><span class="n">add</span><span class="p">(</span><span class="n">len</span><span class="p">),</span><span class="w"> </span><span class="n">remaining_len</span><span class="p">);</span><span class="w"></span>
<span class="w">            </span><span class="bp">self</span><span class="p">.</span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">len</span><span class="p">;</span><span class="w"></span>
<span class="w">            </span><span class="n">ptr</span>::<span class="n">drop_in_place</span><span class="p">(</span><span class="n">s</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Why is that? It's not apparent to me why <code>if len &gt; self.len</code> can't be <code>if len &gt;= self.len</code>.</p>
<p>Thanks!</p>



<a name="222148386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Atruncate%20implementation/near/222148386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation.html#222148386">(Jan 09 2021 at 02:19)</a>:</h4>
<p>From what I see it’s correct but could indeed use &gt;= without problems, I don’t think it would cause any errors</p>



<a name="222153174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Atruncate%20implementation/near/222153174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation.html#222153174">(Jan 09 2021 at 04:30)</a>:</h4>
<p>I believe this is for performance reasons, iirc LLVM is less able to optimize the &gt;= in some cases. I may be confusing it with some other case though.</p>



<a name="222153737"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Atruncate%20implementation/near/222153737" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation.html#222153737">(Jan 09 2021 at 04:47)</a>:</h4>
<p>See the conversation in <a href="https://github.com/rust-lang/rust/pull/78884">https://github.com/rust-lang/rust/pull/78884</a>, <span class="user-mention" data-user-id="307537">@Camelid</span></p>



<a name="222188069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Atruncate%20implementation/near/222188069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation.html#222188069">(Jan 09 2021 at 20:30)</a>:</h4>
<p>Ah, that makes sense. Thanks!</p>



<a name="222188098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Atruncate%20implementation/near/222188098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation.html#222188098">(Jan 09 2021 at 20:31)</a>:</h4>
<p>It is interesting how important it is to look at the actual changes in output when optimizing something – it's very surprising sometimes.</p>



<a name="222188110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Atruncate%20implementation/near/222188110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation.html#222188110">(Jan 09 2021 at 20:31)</a>:</h4>
<p>I actually saw that PR before but I forgot about it <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="222188510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Atruncate%20implementation/near/222188510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation.html#222188510">(Jan 09 2021 at 20:39)</a>:</h4>
<p>I just made a PR (<a href="https://github.com/rust-lang/rust/issues/80857">#80857</a>) to add a comment to <code>Vec::truncate</code> explaining why it uses <code>&gt;</code> and not <code>&gt;=</code>. Hopefully now it will no longer confuse people in the future!</p>



<a name="231552213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/Vec%3A%3Atruncate%20implementation/near/231552213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christian Mazakas <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/Vec.3A.3Atruncate.20implementation.html#231552213">(Mar 23 2021 at 22:12)</a>:</h4>
<p>Huh, I'm having a hard time thinking about the conditional check in <code>truncate()</code> ever being a performance bottleneck. On the surface, this seems like an optimization that solves a non-problem</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>